<template>
  <el-select
    style="width: 100%"
    v-model="comValue"
    :multiple="multiple"
    collapse-tags
    collapse-tags-tooltip
    placeholder="请选择"
    :disabled="disabled"
    :clearable="clearable"
    :filterable="filterable"
  >
    <el-option
      v-for="item in options"
      :key="item.id"
      :label="item.name"
      :value="item.id"
    />
  </el-select>
</template>
<script setup>
import { ref, computed, onBeforeMount, inject } from 'vue'
import { getYLOrgOptions } from '@/api/api'
import { useUserStore } from '@/pinia/modules/user'
const changeTitle = inject("changeTitle",()=>{})
const emit = defineEmits(['update:modelValue', 'change'])
const props = defineProps({
  filterable: {
    type: Boolean,
    default: false
  },
  modelValue: {
    type: String,
    default: ''
  },
  type: {
    type: [String, Number],
    default: ''
  },
  disabled: {
    type: Boolean,
    default: false
  },
  multiple: {
    type: Boolean,
    default: false
  },
  clearable: {
    type: Boolean,
    default: false
  },
  comType: {
    type: String,
    default: ''
  },
  firstValue: {
    type: Boolean,
    default: false
  },
  params: {
    type: Object,
    default: {}
  }
})
const options = ref([])
const comValue = computed({
  get() {
    if (props.multiple) {
      return props.modelValue ? props.modelValue.split(',') : []
    }
    return props.modelValue
  },
  set(val) {
    if (props.multiple) {
      emit('update:modelValue', val.join())
    } else {
      emit('update:modelValue', val)

      let target = options.value.filter(item => item.id == val)[0]
      emit('change', target)
      changeTitle(target)
    }
  }
})
const orgInfo = useUserStore().orgInfo
const allType = {
  service_type_list,
  service_type_list2,
  itemInitiation_orglist_get,
  getYLOrgOptions
}
onBeforeMount(async () => {
  if (props.comType === 'service_type_list') {
    let res = await allType[props.comType]()
    if (res.data && res.data.length > 0) {
      let arr = []
      res.data.forEach((item, index) => {
        arr.push({
          name: item,
          id: index
        })
      })
      options.value = arr
    } else {
      options.value = []
    }
  } else if (props.comType === 'service_type_list2') {
    let res = await allType[props.comType]()
    if (res.data && res.data.length > 0) {
      let arr = []
      res.data.forEach((item, index) => {
        arr.push({
          name: item.name,
          id: item.id
        })
      })
      options.value = arr
    } else {
      options.value = []
    }
  } else if (props.comType === 'getYLOrgOptions') {
    let res = await allType[props.comType]()
    if (res.data && res.data.length > 0) {
      let arr = []
      res.data.forEach((item, index) => {
        arr.push({
          name: item.name,
          id: item.id
        })
      })
      options.value = arr
    } else {
      options.value = []
    }
  }
  
  else {
    let res = await allType[props.comType]({
      pensionOrganizationId: orgInfo.id,
      ...props.params
    })
    options.value = res.data
  }
})
</script>
<style lang="scss" scoped></style>
